<?php
/**
* @software         [JiJinCMS]
* @author           [上海集锦信息科技有限公司]
* @website          [http://www.jijinweb.com]
* @email            [sunny@jijinweb.com]
**/
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use think\facade\Request;
class UpdateCache extends \app\admin\controller\Admin{
    const modelTempPath = 'data/modelTemp/'; //模型表单模板路径    
    const modelTempMemberPath = 'data/modelTempMember/'; //会员模型表单模板路径    
    const templatePath='data/templateFile/';//模板文件所在路径    
    const runtimePath='runtime/';//tp缓存文件
    const uploadCahcePath='public/upload/cache';//上传缓存    
    const chunks_file_cahcePath='public/upload/cache/chunks_file_cahce';//分片文件上传缓存    
    const export_excel_cahcePath='public/upload/cache/export_excel_cahce';//信息导出excel文件缓存
    const import_img_cachePath='public/upload/cache/import_img_cache';//信息导入的excel里包含图片的缓存
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->RouteModel=new \app\admin\model\RouteModel();
        $this->ModelModel=new \app\admin\model\ModelModel();
        $this->CatModel=new \app\admin\model\CatModel();
        $this->TemplateModel=new \app\admin\model\TemplateModel();
        $this->PageModel=new \app\admin\model\PageModel();
    }
    public function ajax(){      
        $urlArr=[
            ["url"=>(string)url('UpdateCache/modelCache'),"msg"=>"模型缓存文件更新成功"],
            ["url"=>(string)url('catCache'),"msg"=>"栏目缓存文件更新成功"],
            ["url"=>(string)url('modelTemp'),"msg"=>"模型表单文件更新成功"],
            ["url"=>(string)url('template'),"msg"=>"模板缓存文件更新成功"],
            ["url"=>(string)url('pageTemplate'),"msg"=>"单模板缓存文件更新成功"],
            ["url"=>(string)url('clearTongji'),"msg"=>"清空统计缓存成功"],
            ["url"=>(string)url('feedbackModel'),"msg"=>"反馈表单缓存文件更新成功"],
            ["url"=>(string)url('route'),"msg"=>"路由缓存文件更新成功"],
            ["url"=>(string)url('UpdateCache/upload'),"msg"=>"更新上传文件缓存文件成功"],
            ["url"=>(string)url('runtime'),"msg"=>"系统缓存文件更新成功"],
        ];
        $this->success($urlArr);
    }
    //clearTongji
    public function clearTongji(){
        //清空48小时以前记录
        $ago_time=time()-172800;
        Db::name("visit_48")->where("visit_time","<",$ago_time)->delete();
        //清空一年以前记录
        $ago_time=time()-(86400*365);
        Db::name("visit")->where("daytime","<",$ago_time)->delete();        
        $this->success("操作成功");
    }    
    //更新模型缓存文件
    public function modelCache(){
        $this->ModelModel->updateCache();
        $this->success("操作成功");
    }
    //更新栏目缓存文件
    public function catCache(){
        $this->CatModel->updateCache();
        $this->success("操作成功");
    }
    //后台模型表单缓存，会员投稿表单缓存
    public function modelTemp(){
        delDirAndFile(base_path().self::modelTempPath);
        delDirAndFile(base_path().self::modelTempMemberPath);
        foreach($this->ModelModel->select() as $v){
            $this->ModelModel->updateModelTemp($v);
            $this->ModelModel->updateModelTempMember($v);
            $this->ModelModel->updateCatModelTemp($v);
        }
        $this->success("操作成功");
    }
    //模板缓存
    public function template(){
        delDirAndFile(base_path().self::templatePath);
        foreach($this->TemplateModel->select() as $v){
            $this->TemplateModel->makeTemplateFile($v["template_id"]);
        }
        $this->success("操作成功");
    }
    //更新单页模板缓存
    public function pageTemplate(){
        foreach($this->PageModel->select() as $v){
            if($v['pagemod']==1){
                $this->PageModel->makePageTemplateFile($v['page_id']);
            }else{
                //如果不是页面式，删除模板文件
                $this->PageModel->deletePageTemplateFile($v['page_id']);
            }
        }
        $this->success("操作成功");
    }         
    //更新留言反馈缓存
    public function feedbackModel(){
//        $cat=M("feedback_cat")->select();
//        $ModelModel=new \Admin\Model\FeedbackCatModel();
//        foreach($cat as $v){
//            $ModelModel->updateModelTemp($v["catid"]);      
//        }
//        $this->success(array(
//            "complete"=>1,
//            "num"=>$this->table->fileNum("./Application/Data/feedbackTemp"),
//        ));
        $this->success("操作成功");
    } 
   
    //更新路由缓存
    public function route(){
        $this->RouteModel->updateCatRoute();
        $this->RouteModel->updateInfoRoute();
        $this->RouteModel->updatePageRoute();
        $this->success("操作成功");
    }
    //删除runtime文件夹
    public function runtime() {
        delDirAndFile(root_path().self::runtimePath,true);
        $this->success("操作成功");
    }  
    //
    public function upload(){
        delDirAndFile(root_path().self::chunks_file_cahcePath,true);        
        delDirAndFile(root_path().self::export_excel_cahcePath,true);
        delDirAndFile(root_path().self::import_img_cachePath,true);
        mkdir(root_path().self::uploadCahcePath);
        mkdir(root_path().self::chunks_file_cahcePath);
        mkdir(root_path().self::export_excel_cahcePath);
        mkdir(root_path().self::import_img_cachePath);
        $this->success("操作成功");
    }
}